package com.stellar.oss.config;


import com.stellar.oss.enums.OssTypeEnum;
import com.stellar.oss.service.AliyunOssStorageService;
import com.stellar.oss.service.MinioStorageService;
import com.stellar.oss.service.StorageService;
import com.stellar.oss.service.TosStorageService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author Jqcc
 * @Version 1.0
 * @Data 2025/3/14
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "storage")
public class StorageConfig {

    private String type;

    private AliyunOssConfig aliyunOss;

    private MinioConfig minio;

    private TosConfig tos;

    @Bean
    public StorageService storageService() {
        if (type == null || type.trim().isEmpty()) {
            throw new IllegalArgumentException("Configuration property 'storage.type' is missing or empty.");
        }
        OssTypeEnum ossType = OssTypeEnum.fromType(type);
        if (OssTypeEnum.ALIYUN_OSS.equals(ossType)) {
            return new AliyunOssStorageService(
                    aliyunOss.getEndpoint(),
                    aliyunOss.getAccessKeyId(),
                    aliyunOss.getAccessKeySecret(),
                    aliyunOss.getBucketName());
        } else if (OssTypeEnum.MINIO.equals(ossType)) {
            return new MinioStorageService(
                    minio.getEndpoint(),
                    minio.getAccessKey(),
                    minio.getSecretKey(),
                    minio.getBucketName());
        } else if (OssTypeEnum.TOS.equals(ossType)) {
            return new TosStorageService(
                    tos.endpoint,
                    tos.region,
                    tos.accessKey,
                    tos.secretKey,
                    tos.bucketName);
        } else {
            throw new IllegalArgumentException("Unsupported storage type: " + type);
        }
    }

    @Data
    public static class AliyunOssConfig {
        private String endpoint;
        private String accessKeyId;
        private String accessKeySecret;
        private String bucketName;
    }

    @Data
    public static class MinioConfig {
        private String endpoint;
        private String accessKey;
        private String secretKey;
        private String bucketName;
    }

    @Data
    public static class TosConfig {
        private String endpoint;
        private String accessKey;
        private String secretKey;
        private String bucketName;
        private String region;
    }
}
